package com.yuwenchao.superafflatus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuwenchao.superafflatus.mapper.SuperLoginRoleUserMapper;
import com.yuwenchao.superafflatus.pojo.entity.SuperLoginRoleUser;
import com.yuwenchao.superafflatus.service.SuperLoginRoleUserService;
import com.yuwenchao.superafflatus.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ChaooGG
 * @since 2021-05-31
 */

@Slf4j
@Api(tags ="用户角色关系信息")
@RestController
@RequestMapping("/superLoginRoleUser")
public class SuperLoginRoleUserController {

    @Autowired
    SuperLoginRoleUserMapper superLoginRoleUserMapper;


    @Autowired
    SuperLoginRoleUserService superLoginRoleUserService;


     //未完成！
    @ApiOperation("根据uid改变用户角色")
    @GetMapping("getRoleUserById")
    public R getRoleUserById(@RequestParam("uids") String uids) {
        log.info(""+uids);
        if(uids.contains(",")) {
            List<Integer> list = new ArrayList<>();
            String[] split =uids.trim().split(",");
            for (String string : split) {
                list.add(Integer.parseInt(string));
            }

              for (Integer uid : list) {
            //先根据uid 查出rid
            QueryWrapper<SuperLoginRoleUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid",uid);
            List<SuperLoginRoleUser> superLoginRoleUsers = superLoginRoleUserMapper.selectList(queryWrapper);
            //判断rid是1管理员  还是2普通用户
            //如果是管理员--》普通用户  删除   添加一条   2
            //如果是普通用户 --》管理员    删除     添加2条  1,2

          /*  if(superLoginRoleUsers.isEmpty()){
                log.info(uid+"该用户还未授权...");
                superLoginRoleUserService.saveRoleUser(uid);
            }*/

            Long rid =  superLoginRoleUsers.get(0).getRid();
            if(rid==1){
                superLoginRoleUserMapper.delete(queryWrapper);
                superLoginRoleUserService.saveRoleUser(uid);
                System.out.println("修改为普通用户成功！");
            }
            if(rid==2) {
                superLoginRoleUserMapper.delete(queryWrapper);
                superLoginRoleUserService.saveSuperRoleUser(uid);
                superLoginRoleUserService.saveRoleUser(uid);
                System.out.println("修改为管理员成功！");

            }
        }
        }else {
            Integer uid = Integer.parseInt(uids);
            System.out.println("单个数据："+uid);
            QueryWrapper<SuperLoginRoleUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid",uid);
            List<SuperLoginRoleUser> superLoginRoleUsers = superLoginRoleUserMapper.selectList(queryWrapper);
          /*  if(superLoginRoleUsers.isEmpty()){
                log.info(uid+"该用户还未授权...");
                superLoginRoleUserService.saveRoleUser(uid);
            }*/
            Long rid =  superLoginRoleUsers.get(0).getRid();
            if(rid==1){
                superLoginRoleUserMapper.delete(queryWrapper);

                superLoginRoleUserService.saveRoleUser(uid);
                System.out.println("修改为普通用户成功！");
            }
            if(rid==2) {
                superLoginRoleUserMapper.delete(queryWrapper);

                superLoginRoleUserService.saveSuperRoleUser(uid);
                superLoginRoleUserService.saveRoleUser(uid);
                System.out.println("修改为管理员成功！");

            }
        }
        return R.ok();
    }




}

